const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  Course,
} = require('../db/model');

class CourseService extends Service {
  async getAllCourse() {
    const res = await Course.findAll().catch(err => {
      console.log(`Course 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`Course 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getCourse(course) {
    if (!course?.cId) {
      console.log(`Course 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${course}`, course);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    const res = await Course.findOne({
      where: {
        cId: course?.cId || -1
      },
    }).catch(err => {
      console.log(`Course 表查找失败: ${err}\n参数: ${course}`, course);
    });
    if (res === undefined) {
      console.log(`Course 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${course}`, course);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`Course 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${course}`, course);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendCourse(course) {
    let res = await this.getCourse(course);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`Course 表新增失败: ${res}\n参数: ${course}`, course);
      return res;
    }
    res = await Course.create(course).catch(err => {
      console.log(`Course 表新增失败: ${err}\n参数: ${course}`, course);
    });
    if (res === undefined) {
      console.log(`Course 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${course}`, course);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateCourse(course) {
    let res = await this.getCourse(course);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Course 表更新失败: ${res}\n参数: ${course}`, course);
      return res;
    }
    res = await Course.update(course, {
      where: {
        cId: course?.cId || -1
      },
    }).catch(err => {
      console.log(`Course 表更新失败: ${err}\n参数: ${course}`, course);
    });
    if (res === undefined) {
      console.log(`Course 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${course}`, course);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyCourse(course) {
    let res = await this.getCourse(course);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Course 表删除失败: ${res}\n参数: ${course}`, course);
      return res;
    }
    res = await Course.destroy({
      where: {
        cId: course?.cId || -1
      },
    }).catch(err => {
      console.log(`Course 表删除失败: ${err}\n参数: ${course}`, course);
    });
    if (res === undefined) {
      console.log(`Course 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${course}`, course);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = CourseService;
